<?php
ob_start();
session_start();

require_once("../configuration.php");
require_once("../includes/db/dbsetup.php");
require_once("../includes/MySmarty.class.php");
include_once("includes/util.php");

$mod = "upload";
	
	if(isset($_GET['mod']))
	{
		$mod = $_GET['mod'];
	}
	if($_SESSION['mod'] == "partnerman")
	{
		$nameimg =  "partner";
	}
	if($_SESSION['mod'] == "news")
	{
		$nameimg =  "news";
	}
	else
	{
		$nameimg =  "product";
	}

	if($mod == "browseall")
	{
		require_once("controller/upload.class.php");	
			
		$upload = new upload($db, $smarty);
		
		$imgs = $upload->getUploadImages();
		
		$smarty->assign("IMAGES", $imgs);
	}
	else
	{
		if($_FILES['userfile']['size'] != 0)
		{
			require_once("controller/upload.class.php");	
			
			$upload = new upload($db, $smarty);
			
			if($_POST['mod'] != "downloadman")
			{
				$image = $_FILES['userfile']['name'];
				$path = pathinfo($image);
				$image_type = strtolower($path['extension']);
				if($image_type != "jpeg" && $image_type != "jpg" && $image_type != "jpe" && $image_type != "gif" && $image_type != "png" && $image_type != "bmp" && $image_type != "pdf")
				{
					$_SESSION['error_msg'] = "Image must be a jpeg, gif, png or bmp file.";
				}
				/*if($_FILES['userfile']['size'] > 1048576)
				{
					if($_SESSION['error_msg'] != "") $_SESSION['error_msg'] .= "<br />";
					$_SESSION['error_msg'] .= "Maximum file size is 1 MB.";
				}*/
				if(!isset($_SESSION['error_msg']))
				{
					$name = $upload->uploadFile($nameimg, $_FILES['userfile']['tmp_name'], $image_type);
					if($name != "")
					{
						$_SESSION['file'] = array($name);
						$_SESSION['type'] = $image_type;
						ob_clean();
						header("Location: ./upload_image.php?mod=browse&target=$_GET[target]");
					}
				}
			}
			else
			{
				$image = $_FILES['userfile']['name'];
				$path = pathinfo($image);
				$type = strtolower($path['extension']);
				if($type != "pdf")
				{
					$_SESSION['error_msg'] .= "Only accept pdf file.";
				}
				else
				{
					echo "adadasd";
					$randomname = $image;//$photo.date("YmdHms").mt_rand();		
					$$name = $image;//md5($randomname).".$type";				
					$result = move_uploaded_file($_FILES['userfile']['tmp_name'], "../".UPLOAD_DIR_STORE_DOWNLOAD.$$name);
	         		if($result == 1)
	         		{
	         			$_SESSION['file'] = array($$name);
	         			$_SESSION['type'] = $image_type;
	         			$_SESSION['name'] = $$name;
						ob_clean();
						header("Location: ./upload_image.php?mod=browse&target=$_GET[target]&modfile=downloadman");
	         		} 
	         		else
	         		{
	         			$_SESSION['error_msg'] .= "Problem when uploading file. Please try again.";
	         		}
				}
			}
		}
		
	}	
	if(isset($_SESSION['error_msg']))
	{
		$smarty->assign("error_msg", $_SESSION['error_msg']);
		unset($_SESSION['error_msg']);
	}
	$smarty->display("admin/upload_image.tpl");
ob_end_flush();
?>